package com.hamster.crm.web;

import com.hamster.crm.pojo.ProjectCourse;
import com.hamster.crm.service.ProjectCourseService;
import com.hamster.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目中的课程的Controller
 * 【项目课程页面】
 * @author Tarry
 * @create 2019/12/24 13:58
 */
@RestController
@RequestMapping("projectCourse")
public class ProjectCourseController {
    @Autowired
    private ProjectCourseService projectCourseService;

    /**
     * 添加项目课程【项目课程页面】
     * @param projectCourse
     * @return
     */
    @PostMapping("insert")
    public ResponseEntity<Void> insert(@RequestBody ProjectCourse projectCourse) {
        projectCourseService.insert(projectCourse);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除项目课程【项目课程页面】
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public ResponseEntity<Void> delete(@RequestBody List<String> ids){
        projectCourseService.delete(ids);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id获取单个项目课程
     * @param id 项目课程的id
     * @return ProjectCourse 课程详情
     */
    @GetMapping("get")
    public ResponseEntity<ProjectCourse> get(@RequestParam String id){
        ProjectCourse projectCourse = projectCourseService.get(id);
        return ResponseEntity.ok(projectCourse);
    }

    /**
     * 修改项目课程
     * @param projectCourse
     * @return
     */
    @PostMapping("update")
    public ResponseEntity<Void> update(@RequestBody ProjectCourse projectCourse){
        projectCourseService.update(projectCourse);
        return ResponseEntity.ok().build();
    }

    /**
     * 条件查找项目课程列表
     * @param pageSize     页面条数
     * @param currentPage   当前页号
     * @param title         课程名称
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<PageResult<ProjectCourse>> listProjectCourses(
            @RequestParam(required=true) Integer pageSize,
            @RequestParam(required=true) Integer currentPage,
            @RequestParam(required = false) String title
    ){
        PageResult<ProjectCourse> result = projectCourseService.listProjectCourses(pageSize,currentPage,title);

        return ResponseEntity.ok(result);
    }

    /**
     * 【添加项目页面需要展示所有课程】
     * 下拉框获取，下拉列表内容
     * @return
     */
    @GetMapping("listOption")
    public ResponseEntity<List<ProjectCourse>> listOptions(){
        List<ProjectCourse> projectCourses = projectCourseService.listOptions();
        return ResponseEntity.ok(projectCourses);
    }

}
